from tortoise import fields

from .base import BaseModel


class User(BaseModel):
    """User model for authentication and authorization."""

    username = fields.CharField(max_length=50, unique=True)
    email = fields.CharField(max_length=255, unique=True)
    hashed_password = fields.CharField(max_length=255)
    is_active = fields.BooleanField(default=True)
    is_superuser = fields.BooleanField(default=False)

    # Reverse relations
    roles = fields.ManyToManyField("models.Role",
                                   through="user_roles",
                                   related_name="users",
                                   description="Roles assigned to this user")

    class Meta:
        table = "users"

    def __str__(self):
        return f"{self.username} ({self.email})"
